package com.lanhai.zhonglai.third;

import com.lanhai.zhonglai.beans.StationDataBean;
import com.lanhai.zhonglai.beans.StationMountBean;
import com.lanhai.zhonglai.enums.TransDirectionEnum;
import com.lanhai.zhonglai.enums.StationDataTypeEnum;
import com.lanhai.zhonglai.interfaces.IStationProtocol;
import com.lanhai.zhonglai.utils.Tools;
 
import java.util.Arrays;
import java.util.List;

/**
 * 扩散（先导使用） 注意，进料口和出料口都是同一个IP， 程序上约定进料口线别是1，出料口线别是2
 * 
 * @author zhengwei
 *
 */
public class KuoSan implements IStationProtocol {
	@Override
	public byte[] agvArrived(byte line, TransDirectionEnum type) {
		byte[] buf = new byte[8];
		if(type==TransDirectionEnum.OnlyIn) {
			buf[0]=1;
		}else {
			buf[0]=2;
		}
		buf[2] = 1; 
		return buf;
	}

	@Override
	public byte[] applyLeave(byte line, TransDirectionEnum type) {
		byte[] buf = new byte[8];
		if(type==TransDirectionEnum.OnlyIn) {
			buf[0]=1;
		}else {
			buf[0]=2;
		}
		buf[2] =2; 
		return buf;
	}

	@Override
	public List<StationDataBean> analyseData(String ip, byte[] buf) {
		// 先解析左边四个（进料）

		byte[] inbuf = new byte[] { buf[0], buf[1], buf[2], buf[3] };
		byte[] outbuf = new byte[] { buf[4], buf[5], buf[6], buf[7] };

		StationDataBean objin = getBean(inbuf, true);
		objin.setCmdStr(Tools.byte2HexStr(buf));
		objin.setIp(ip);

		StationDataBean objout = getBean(outbuf, false);
		objout.setCmdStr(Tools.byte2HexStr(buf));
		objout.setIp(ip);
 
		return Arrays.asList(objin, objout);
	}

	StationDataBean getBean(byte[] buf, boolean in) {
		StationDataBean obj = new StationDataBean();
		if (in) {
			// 进料
			obj.setLine((byte) 1); 
		} else {
			// 出料
			obj.setLine((byte) 2); 
		}
		if (buf[0] == 4) {
			// 机台异常
			obj.setEventType(StationDataTypeEnum.Error);
		} else if (buf[0] == 3) {
			// 允许离开
			obj.setEventType(StationDataTypeEnum.AllowLeave);
		} else if (buf[0] == 2) {
			// 允许传输
			obj.setEventType(StationDataTypeEnum.AllowTrans);
		} else if (buf[0] == 5) {
			// 允许传动
			obj.setEventType(StationDataTypeEnum.Receive);
		}else {
			obj.setEventType(StationDataTypeEnum.Info);
			// 上下料
			int num = buf[2];
			StationMountBean mb = new StationMountBean();
			if (in) {
				// 进料
				obj.setLine((byte) 1);
				mb.setInNum(num);
				if (num == 0) {
					mb.setAllowIn(true);
				}
			} else {
				// 出料
				obj.setLine((byte) 2);
				mb.setOutNum(num);
				if (num == 10) {
					mb.setAllowOut(true);
				}
			}
			obj.setBean(mb);
		} 
		return obj;
	}

	@Override
	public byte[] agvLeave(byte line, TransDirectionEnum type) {
		byte[] buf = new byte[8];
		if(type==TransDirectionEnum.OnlyIn) {
			buf[0]=1;
		}else {
			buf[0]=2;
		} 
		return buf;
	}

	@Override
	public byte[] applyRecieve(byte line, TransDirectionEnum type) {
		byte[] buf = new byte[8];
		if(type==TransDirectionEnum.OnlyIn) {
			buf[0]=1;
		}else {
			buf[0]=2;
		}
		buf[2] = 3; 
		return buf;
	}
	@Override
	public byte[] transError(byte line, TransDirectionEnum type) {
		byte[] buf = new byte[8];
		if(type==TransDirectionEnum.OnlyIn) {
			buf[0]=1;
		}else {
			buf[0]=2;
		} 
		return buf;
	}
}
